package org.weasel.business;

import java.io.File;
import java.sql.Timestamp;

import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.weasel.views.Navigator;

public class Utilities {
	/**
	 * This method retrieves the actual Navigator object and calls the createAndUpdateTree
	 * method to make changes visible in the tree.
	 */
	public static void callCreateAndUpdateTree() {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewPart viewPart = page.findView(Navigator.ID);
		Navigator navigator = (Navigator) viewPart;
		navigator.createAndUpdateTree();
	}
	
	//TODO refactor this method
	public static String getDate(){
		Timestamp dateTime = new Timestamp(System.currentTimeMillis());
	    // Following two lines used to cut away the milliseconds in dateTime 
	    String tmp=dateTime.toString();
	    String date = tmp.substring(8,10) + "." + tmp.substring(5, 7) + "." + tmp.substring(0,4);
	    return date;
	}
	
	//TODO refactor this method
	public static String getTime(){
		Timestamp dateTime = new Timestamp(System.currentTimeMillis());
	    // Following two lines used to cut away the milliseconds in dateTime 
	    String tmp=dateTime.toString();
	    String time = tmp.substring(11, 19);
	    return time;
	}
	
	@Deprecated
	public static String getTimestamp(){
		Timestamp timestmp = new Timestamp(System.currentTimeMillis());
		return timestmp.toString();
	}
	
	/**
	 * Creates a unique id. It cannot just be returned the System.currentTimeMillis()
	 * since the granularity goes down to about 10ms, so there is the risk of getting
	 * duplicate id's
	 * @return
	 */
	public static synchronized String createUniqueId(){
		long id = System.nanoTime();
		return String.valueOf(id);
	}
	
	public static boolean existsFile(String url){
		File f = new File(url);
		return f.exists();
	}
	
	public static boolean deleteFile(String url){
		File f = new File(url);
		return f.delete();
	}
}